Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

221
Vistas
[Node.js]How to access files in different directory using readFile

Under directory main:

  • css(foler)
    • stylesheet.css
  • js(folder)
    • data.js
    • server.js
  • index.html

And inside server.js:


    //Cannot access index.html
    file.readFile('index.html', 'utf8', function(err, contents){
      //some code
    });

Specifically, how do we access stylesheet.css and index.html

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

For accessing folders on a top level directory you can use .. in your path.

Access index.html in server.js

//access top level directory using `..`
file.readFile('../index.html', 'utf8', function(err, contents){
  //some code
});

file.readFile('../css/stylesheet.css', 'utf8', function(err, contents){
  //some code
});

Ideally you would always import from absolute paths. That prevents breaking your app when you restructure it. I suggest to always start your app from the root directory of your project like node js/server.js .

When starting it from root the process.cwd() (current working directory.) variable will always point to the root dir of the project and file reads could be done using:

var { join } = require('path')
var htmlFile = join(process.cwd(), 'index.html')
var cssFile = join(process.cwd(), 'css', 'stylesheet.css')

// read html file
file.readFile(htmlFile, 'utf8', function(err, contents){
  //some code
});

// ...
about 4 years ago · Juan Pablo Isaza Denunciar

0

when you start the server, a global variable called __dirname is set that holds the absolute path of the folder that server.js is inside. Now we can use that value to read files relative to server.js file. In the following code, I used path.join() to construct the path to the files that you want to access. The double dots (..) is the directory above the js folder.

const fs = require("fs");
const path = require("path");

// Get the absulote path of the files
const htmlFile = path.join(__dirname, "..", "index.html");
const cssFile = path.join(__dirname, "..", "css", "stylesheet.css");

// Read files
fs.readFile(htmlFile, "utf8", function (err, content) {
  console.log(content);
});

fs.readFile(cssFile, "utf8", function (err, content) {
  console.log(content);
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

Try add ".", so it will be :

fs.readFile("./../index.html", "utf8", (err, content){ return }) 
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda